

*------------------------------------------------------------------------------------------------------------%	
* Table 3: Estimated spillover from large retailer VMWs on connected non-policy establishments
*------------------------------------------------------------------------------------------------------------%	
	use "$data/cb/events.dta", clear
	rename eventid trt_exp
	rename cmp_company_code policy_cmp_company_code
	keep trt_exp policy_cmp_company_code
	tempfile events
	save `events'
	
	* Load nonpolicy firm data
	use using "$data/cb/stacked_nonpolicy_firm_dataset.dta" if balanced_long==1 , clear
	drop if trt_exp==4
	keep if inrange(etime, -12, 11)
	
	* Get policy company
	merge m:1 trt_exp using `events', keepusing(policy_cmp_company_code) keep(1 3)
	
	* Create hiring variables
	drop all_new_hires new_hires*
	merge m:1 cmp_company_code cz mdate using "$data/cb/raw_policy_to_nonpolicy_new_hires_collapsed.dta", keep(1 3) nogen
	foreach var of varlist all_new_hires new_hires* {
		replace `var' = 0 if missing(`var')
	}
	gen pr_new_hires = all_new_hires > 0 & !missing(all_new_hires)
	gen pr_pol_hires = 0
	gen pr_np_hires = 0
	gen ln_np_hires = .
	gen ln_pol_hires = .
	gen pol_hire_share = .
	levelsof policy_cmp, local(pol)
	foreach cmp in `pol' {
		replace pr_pol_hires = 1 if policy_cmp == `cmp' & new_hires`cmp' > 0
		replace pr_np_hires = 1 if policy_cmp == `cmp' & new_hires`cmp' < all_new_hires
		replace ln_np_hires = ln(all_new_hires - new_hires`cmp') if policy_cmp == `cmp'
		replace ln_pol_hires = ln(new_hires`cmp') if policy_cmp == `cmp'
		replace pol_hire_share = new_hires`cmp'/all_new_hires if policy_cmp == `cmp'
	}
	gen ln_all_new_hires = ln(all_new_hires)
	
	* Indicator for positive flows
	gen pos_flows_estab = (feeder_estab == 1 | poaching_estab == 1)
	
	keep if pos_flows_estab==1 
	
	* Indicator for post policy
	gen postperiod = etime>=0

* No interactions 1 ------------------------------------------------------------

		eststo clear

		foreach outcome of varlist pr_new_hires pr_pol_hires pr_np_hires {

			* All
			preserve
			qui sum T,d
			local std_T=round(r(sd),.001)
			
			egen T_std=std(T)
			replace T=T_std		
			
			eststo a`outcome'1: reghdfe `outcome' c.T#1.postperiod if balanced_short==1 & pos_flows_estab==1, absorb(i.cmp_company_code#i.cz#i.trt_exp i.cmp_company_code##i.trt_exp i.cz##i.trt_exp i.etime##i.trt_exp) cluster(cz)
			estadd local Tstd `std_T'
			distinct cz if e(sample) == 1
			estadd local Ncz `r(ndistinct)'
			sum `outcome' if e(sample) == 1 & postperiod == 0
			estadd local preMean = round(`r(mean)', 0.01)
			estadd local cmpXCZFE "Y"
			estadd local etimeFE "Y"
			restore
			

		}
	
		* Build table
		
		#d ;
		esttab a* 
				using "$figures_tables/table3_spillover_hire_prob_poach_or_feed_all.tex",  
				replace label fragment
				nolines  
				prehead("\begin{threeparttable} \begin{tabular}{lccc} \toprule \toprule & \multicolumn{3}{c}{Dep Var: Probability of new hires} \\ Independent variable") 
				posthead(\cmidrule(lr){2-4}) 
				booktabs								
				nonumbers 
				mtitles("Overall" "From Large Retailer" "From Others") 
				collabels(none)  
				cells(b(star fmt(%9.4f)) se(par fmt(%9.4f)) ) 
				starlevels(* 0.1 ** 0.05 *** 0.01) 						
				keep(1.postperiod#c.T)   
				coeflabel(1.postperiod#c.T "Large retailer gap X 1(Post)") 
				stats(
					N Ncz Tstd preMean cmpXCZFE etimeFE, 
					fmt(%12.0gc) 
					label("Obs" "CZs" "St. dev. large retailer gap" "Dep var pre-treat mean" "Company X CZ FEs" "Month from event FEs")
				)
				prefoot(\midrule) 
				postfoot(\bottomrule \bottomrule \end{tabular} \end{threeparttable} )
		;
		#d cr	
